<?php 
	namespace app\admin\controller;
	/**
	* 新闻分类
	*/
	class Newcate extends Base
	{
		//新闻分类的展示
		public function nc_list(){
			$nc=model("TkNewCates");
			$dataobj=$nc->select();
			if(count($dataobj)==0){
				$this->success("请先添加商品","add_new_cates");
			}
			foreach ($dataobj as $v) {
				$data[]=$v->toArray();
			}
			$data=$this->getcates($data);
			foreach ($data as $k=>$v) {
				$data[$k]['pc_name']=str_repeat("&nbsp;&nbsp;--", $v["pc_level"]-1).$v["pc_name"];
			}
			// print_r($data);;
			$this->assign("data",$data);
			return $this->fetch();
		}

		//添加新闻分类
		public function add_new_cates(){
			$pc=model("TkNewCates");
			if(request()->isPost()){
				// print_r($_POST);
				$pc_f_id=input("param.pc_f_id");
				if($pc_f_id==0){
					$pc_level=1;	
				}else{
					$d=$pc->find($pc_f_id)->toArray();
					$pc_level=$d['pc_level']+1;
				}	
				$_POST["pc_level"]=$pc_level;
				$num=$pc->allowField(true)->save($_POST);
				if($num){
					$this->success("添加成功",'nc_list','',1);
				}else{
					$this->error("请重新添加",'nc_list','',1);
				}
			}else{
				$cateobj=$pc->select();
				if(count($cateobj)==0){
					$cates=[];
				}else{
					// $city=model("TkCity");
					foreach ($cateobj as $key => $value) {
						$cates[]=$value->toArray();
					}
					foreach ($cates as $k=>$v) {
						$cates[$k]['pc_name']=str_repeat("&nbsp;&nbsp;--", $v["pc_level"]-1).$v["pc_name"];
					}
					$cates=$this->getcates($cates);
				}
				$this->assign("cates",$cates);
				return $this->fetch();
			}
		}

		//新闻分类删除
		public function del_new_cates(){
			$pc=model("TkNewCates");
			$pro=model("TKNew");
			$id=input("param.id");
			$dataobj=$pc->select();
			foreach ($dataobj as $v) {
				$data[]=$v->toArray();
			}
			foreach ($data as $key => $value) {
				if($id==$v['pc_f_id']){
					$this->success("请先删除子类",'nc_list','',1);		
				}
			}
			$num2=$pro->where("pc_id=$id")->delete();
			$num=$pc->destroy($id);
			if($num){
				$this->success("删除成功",'nc_list','',1);
			}else{
				$this->error("请重新删除",'nc_list','',1);
			}
		}

		//新闻分类编辑
		public function edit_new_cates(){
			$pc=model("TkNewCates");
			if(request()->isPost()){
			 	$pc_f_id=input("param.pc_f_id");
				$dataobj=$pc->where("id=$pc_f_id")->select();
				foreach ($dataobj as $key => $value) {
					$data[]=$value->toArray();
				}
				$pc_level=$data[0]['pc_level']+1;
				$_POST['pc_level']=$pc_level;
			 	$num=$pc->allowField(true)->save($_POST,['id'=>input("param.id")]);
				if($num){
					$this->success("修改成功",'nc_list','',1);
				}else{
					$this->error("请重新修改",'','',1);
				}
			}else{
				$id=input("param.id");
				$data=$pc->find($id)->toArray();
				$cateobj=$pc->select();
				foreach ($cateobj as $key => $value) {
					$cates[]=$value->toArray();
				}
				foreach ($cates as $k=>$v) {
					$cates[$k]['pc_name']=str_repeat("&nbsp;&nbsp;--",$v["pc_level"]-1).$v["pc_name"];
				}
				$this->assign("cates",$cates);
				$this->assign("data",$data);
				return $this->fetch();
			}
		}

		//上下架
		public function pro_cates_ifshow(){
		$id=input("param.id");
		$pc=model("TkNewCates");
		$pro=model("TkNew");
		$dataobj=$pc->select();
		foreach ($dataobj as $v) {
			$data[]=$v->toArray();
		}
		//print_r($data);
		foreach ($data as $key => $value) {
			if($id==$v['pc_f_id']){
				$this->success("请先下架子类",'nc_list','',1);		
			}
		}
		$ifshow=input("param.ifshow");
		if($ifshow==1){
			$ifshow=0;
		}else{
			$ifshow=1;
		}
		$pc=model("TkNewCates");
		$data=[
			"ifshow"=>$ifshow,
		];
		$num=$pc->where("id=$id")->update($data);
		$num2=$pro->where("pc_id=$id")->update($data);
		// echo $num;
		// echo $pc->getLastSql();
		// exit;
		if($num){
			$this->success("修改成功",'nc_list','',1);
		}else{
			$this->error("请重新修改",'nc_list','',1);
		}
	}
	}
?>